
#include "tcpsrv.h"
#include "tcphandler.h"
#include "inifile.h"


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/time.h>
#include <errno.h>

#define MAX_SOURCES		10
#define IP_FIELD_LENGTH	16

extern struct tcphandler gc_rtsphandler;
struct tcpsrv *gpServer = NULL;
int gsrc_count = 0;
int gsrc_port = 0; 
char gsrc_ips[MAX_SOURCES][IP_FIELD_LENGTH];

void timer_init();
void timer_stop(); 
void timer_handler(int s);

void sigint_handler(int s) 
{
	//timer_stop();
	tcpsrv_stop(gpServer);
	exit(0);
}


int main(int argc, char **argv) 
{
	int ret = 0;
	char inifile[256];
	char *szval = NULL;
    int port = 554;
	int max_connections = 100;
	double timeout = 3.;
	char ipname[16];
	// char *tmp=NULL;
	// int offset = 0;
	int count = 0;
	int i = 0;

	//if(argc < 2)
	//{
	//	printf("Usage: %s <ini filename>\n", argv[0]);
	//	exit(0);
	//}

	//memset(inifile, 0, sizeof(inifile));
	//strcpy(inifile, argv[1]);
	strcpy(inifile, "./odrm_conf.ini");

	szval = GetIniSectionItem(inifile, "server", "tcp_port");
	if(szval)
		port = atoi(szval);
	szval = GetIniSectionItem(inifile, "server", "connect_max_count");
	if(szval) 
		max_connections = atoi(szval);
	szval = GetIniSectionItem(inifile, "server", "connect_timeout");
	if(szval) 
		timeout = atof(szval);

	szval = GetIniSectionItem(inifile, "sources", "count");
	if(szval) 
		gsrc_count = atoi(szval);
	szval = GetIniSectionItem(inifile, "sources", "port");
	if(szval) 
		gsrc_port = atoi(szval);
	count = (gsrc_count <= MAX_SOURCES) ? gsrc_count : MAX_SOURCES;
	for(i=0; i<count; i++)
	{
		sprintf(ipname, "ip%d", i);
		szval = GetIniSectionItem(inifile, "sources", ipname);
		if(szval) 
		{
//			memset(gsrc_ips[i], 0, sizeof(gsrc_ips[i]));
			strcpy((char*)gsrc_ips[i], szval);
		}
	}

	printf("source count=%d, port=%d, ip0=%s, ip1=%s\n", gsrc_count, gsrc_port,gsrc_ips[0], gsrc_ips[1] );
	init_logs("./odrm0.log", LOGL_INFO, LOG_OUTPUT_STDOUT|LOG_OUTPUT_FILE);

	gpServer = tcpsrv_init(port, &gc_rtsphandler, max_connections, timeout);
	if(!gpServer) goto eout;

	//timer_init();

	// ignore SIGPIPE ......
	signal(SIGPIPE, SIG_IGN);
	signal(SIGCHLD, SIG_IGN); // ignore child
	signal(SIGTSTP, SIG_IGN); // ignore tty signals
	signal(SIGTTOU, SIG_IGN);
	signal(SIGTTIN, SIG_IGN);
	signal(SIGHUP,  SIG_IGN); // ignore hangup signal


    // handle C-c
    struct sigaction on_sigint;
    on_sigint.sa_handler = sigint_handler;
    sigemptyset(&on_sigint.sa_mask);
    on_sigint.sa_flags = 0;
    sigaction(SIGINT, &on_sigint, NULL);


	ret = tcpsrv_run_loop(gpServer);

eout:
	//timer_stop();
	uninit_logs();
	return ret;
}


//---------------timer --------------//
#define TSDATA_READ_TIMER   50000	// 50ms

void timer_init()
{
	struct itimerval value;
 	struct sigaction tact;
	
	value.it_value.tv_sec = 0;
	value.it_value.tv_usec = TSDATA_READ_TIMER;	
	value.it_interval = value.it_value;
	setitimer(ITIMER_REAL, &value, NULL);
	
	tact.sa_handler = timer_handler;
	tact.sa_flags = 0;
	sigemptyset(&tact.sa_mask);
	sigaction(SIGALRM, &tact, NULL);
}
 
void timer_stop()  
{  
	 struct itimerval value;  
	 value.it_value.tv_sec = 0;  
	 value.it_value.tv_usec = 0;  
	 value.it_interval = value.it_value;  
	 setitimer(ITIMER_REAL, &value, NULL);  
} 

void timer_handler(int s)
{
}

